package com.zzyl.task;


import com.zzyl.base.BaseEntity;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义定时任务类
 */
@Component
@Slf4j
public class ReservationTask {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * // 每小时的1分钟和31分钟执行
     */
    @Scheduled(cron = "0 1,31 * * * ?")
    @PostConstruct
    public void executeTask() {
        log.info("----定时任务开始执行----：{}", LocalDateTime.now());

        //定时查询时间过期的预约信息
        Integer status = 0; //待报到
        List<Reservation> reservations = reservationMapper.selectByStatus(status, LocalDateTime.now());

        if (reservations != null && reservations.size() != 0) {
            //获取预约记录的ID集合
            List<Long> ids = reservations.stream().map(BaseEntity::getId).collect(Collectors.toList());
            //更新预约状态时间为已过期
            reservationMapper.updateByIds(ids);
        }

    }
}
